package com.itheima.service.impl;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectPivot;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishMpper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceimpl extends ServiceImpl<DishMpper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        save(dishDto);

        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(item -> item.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    //删除菜品
    @Override
    public void DeleteByIds(Long[] ids) {
        /*LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.in(Dish::getId, ids).eq(Dish::getStatus, 0);

        LambdaQueryWrapper<DishFlavor> dishFlavorlqw = new LambdaQueryWrapper<>();
        dishFlavorlqw.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorlqw);

        remove(dishlqw);*/
        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.in(Dish::getId, ids);
        List<Dish> list = dishService.list(dishlqw);
        for (Dish dish : list) {
            if (dish.getStatus() == 1) {
                throw new CustomException("您选中的菜正在售卖中,不能删除!");
            }
        }
        remove(dishlqw);
        LambdaQueryWrapper<DishFlavor> dishFlavorlqw = new LambdaQueryWrapper<>();
        dishFlavorlqw.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorlqw);

    }

    //修改状态
    @Override
    public void UpdateStatusById(Integer status, Long[] ids) {
        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.in(Dish::getId, ids);
        List<Dish> dishlist = dishService.list(dishlqw);
        for (int i = 0; i < dishlist.size(); i++) {
            dishlist.get(i).setStatus(status);
        }
        dishService.updateBatchById(dishlist);
    }


}
